package com.qiantu.phone.ui.dialog;

import android.content.Context;

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;
import com.lxj.xpopup.core.AttachPopupView;

import java.util.List;

public class OptionDialog extends AttachPopupView {
    private List<Option> mOptions;
    private OnItemClickListener mListener;
    private RecyclerView optionList;
    private OptionAdapter optionAdapter;

    public OptionDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_option_ly;
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @Override
    protected void onCreate() {
        super.onCreate();
        optionList = (RecyclerView) findViewById(R.id.option_list);
        optionAdapter = new OptionAdapter();
        optionList.setLayoutManager(new LinearLayoutManager(getContext()));
        optionList.setAdapter(optionAdapter);
        optionAdapter.setNewInstance(mOptions);
        optionAdapter.setOnItemClickListener(mListener);
    }

    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }

    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }

    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    protected int getPopupHeight() {
        return 0;
    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    private class OptionAdapter extends BaseQuickAdapter<Option, BaseViewHolder>{

        public OptionAdapter() {
            super(R.layout.option_item);
        }

        @Override
        protected void convert(BaseViewHolder helper, Option item) {
            helper.setImageDrawable(R.id.icon, AppApplication.getInstance().getThemeDrawable(item.icon));
            helper.setText(R.id.title, item.title);
            if(item.color != 0){
                helper.setTextColor(R.id.title, item.color);
            }
        }
    }

    public static class Option {
        public @DrawableRes int icon;
        public String title;
        private @ColorInt int color;

        public Option(int icon, String title) {
            this.icon = icon;
            this.title = title;
        }

        public Option(int icon, String title, int color) {
            this.icon = icon;
            this.title = title;
            this.color = color;
        }
    }

    public OptionDialog setOptions(List<Option> options){
        mOptions = options;
        if (optionAdapter!=null){
            optionAdapter.setNewInstance(options);
        }
        return this;
    }

    public OptionDialog setListener(OnItemClickListener listener){
        mListener = listener;
        if (optionAdapter!=null){
            optionAdapter.setOnItemClickListener(mListener);
        }
        return this;
    }
}
